import tkinter as tk
from tkinter import ttk
import serial.tools.list_ports
from VarBind import VarBind
class PortFrame(tk.LabelFrame):
    def __init__(self,*argv,**kwards):
        super().__init__(*argv,**kwards)
        self.tmp = tk.StringVar()
        self._design()
    def _design(self):
        self.l_port=tk.Label(self,text="端口号")
        self.e_port=ComboboxMax(self,varbind=self.tmp)
        self.b_openport=tk.Button(self,text="打开串口")
        self.b_portset = tk.Button(self,text="更多串口设置")
        self.c_rts = tk.Checkbutton(self,text="RTS")
        self.c_dtr = tk.Checkbutton(self,text="DTR")
        self.l_bound= tk.Label(self,text="波特率:")
        self.cb_bound=ttk.Combobox(self,values=["9600","14400","19200","115200"],width=7)
        self.b_Send=tk.Button(self,text=">发送<")
        ...

        self._layout()
    def _layout(self):
        self.l_port.grid(column=0,row=0)
        self.e_port.grid(column=1,row=0,columnspan=3)
        self.b_openport.grid(column=1,row=1,columnspan=2)
        self.b_portset.grid(column=3,row=1)
        self.c_rts.grid(column=0,row=2)
        self.c_dtr.grid(column=1,row=2)
        self.l_bound.grid(column=2,row=2)
        self.cb_bound.grid(column=3,row=2)
        self.b_Send.grid(column=0,row=3,columnspan=4,sticky="we")
        ...
    def m_SetButtonState(self,state:bool):
        self.b_openport.configure(text=["关闭串口","打开串口"][state])
        self.e_port.SetStateOption(state)
    def m_BindVar(self,var:VarBind):
        self.c_rts.configure(variable=var.vardict_bool["rts_bool"])
        self.c_dtr.configure(variable=var.vardict_bool["dtr_bool"])
        self.cb_bound.configure(textvariable=var.vardict_int["bound_int"])
class ComboboxMax(tk.LabelFrame):
    def __init__(self,*argv,varbind,**kwards):
        super().__init__(*argv,**kwards)
        self.vv  = varbind
        self.stateoption=True
        self.butin=[]
        self._design()
        self._event()
    def _design(self):
        self.xx=tk.Entry(self,textvariable=self.vv,width=20,state="normal")#
        self.xx.grid(column=0,row=0,sticky="w")
        self.columnconfigure(0,weight=1)
        self.xx["bg"]="#FFFFFF"

    def _event(self):
        
        self.xx.bind("<Button-1>",self.selectWindow)

    def selectWindow(self,e):
        self.butin=[]
        port_list = list(serial.tools.list_ports.comports())
        for idx,idy,_ in port_list:
            self.butin.append(f"{idx} {idy}")
        
        if self.stateoption:
            self.vv.set("")
            zx = tpsecelt(self,self.xx.winfo_rootx(),self.xx.winfo_rooty(),self.butin)
        
    def SetStateOption(self,state:bool):
        self.stateoption=state
        if self.stateoption:
            self.xx["bg"]="#F0F0F0"
        else:
            self.xx["bg"]="#a0FFa0"
       # print(self.xx.winfo_rootx())
        # print(self.xx.winfo_rooty())
        # zz = tk.Toplevel(self)
        # zz.wm_geometry()
class tpsecelt:
    def __init__(self,master,x,y,butlist=["1-------------------------------------------sssssssdasdasd-------","2"]) -> None:
        self.mavv =master.vv
        self.zz = tk.Toplevel(master)
        self.zz.attributes('-topmost', True)
        self.zz.grab_set()
        self.zz.wm_overrideredirect(1)
        self.zz.wm_geometry(f"500x180+{x}+{y}")
        self.xx=tk.Listbox(self.zz,selectmode="SINGLE")
        for idx in butlist:
            self.xx.insert('end',idx)
        self.xx.bind('<<ListboxSelect>>',self.Gui_BindDeal)
        self.xx.pack(fill=tk.X)
    def Gui_BindDeal(self,e):
        if self.xx.curselection()!=():
            ...
            print(e)
            print(self.xx.get(self.xx.curselection()))
            self.mavv.set(self.xx.get(self.xx.curselection()))
        self.zz.destroy()
if __name__=="__main__":
    win = tk.Tk()

    xx= PortFrame(win)
    xx.pack()
    cc = tk.Button(win,command=lambda x=False: xx.m_SetButtonState(x))
    cc.pack()

    win.mainloop()